"""
初始化
"""
from loguru import Logger
from toollib.utils import Singleton

from leoz.space.idcard.initializer.conf import init_conf
from leoz.space.idcard.initializer.logger import init_logger


class G(metaclass=Singleton):
    """
    全局变量
    """
    conf = None
    logger: Logger = None
    # snow: SnowFlake = None
    # redis: RedisCli = None
    # db_session: scoped_session = None
    # db_async_session: sessionmaker = None

    def setup(self):
        """
        初始化
        """
        # 注意：初始化顺序
        self.conf = init_conf()
        self.logger = init_logger(
            level=self.conf.get('loging', {}).get('level'),
            log_dir=self.conf.get('loging', {}).get('log_dir'),
        )
        # self.snow = init_snow(
        #     worker_id=self.conf.snow_worker_id,
        #     datacenter_id=self.conf.snow_datacenter_id,
        # )
        # self.redis = init_redis(
        #     host=self.conf.redis_host,
        #     port=self.conf.redis_port,
        #     db=self.conf.redis_db,
        #     password=self.conf.redis_password,
        # )
        # self.db_session = init_db(
        #     db_url=self.conf.db_url,
        #     db_echo=self.conf.debug,
        # )
        # self.db_async_session = init_db_async(
        #     db_url=self.conf.db_async_url,
        #     db_echo=self.conf.debug,
        # )


g = G()
# 建议：
# 为了避免G下的全局变量在未初始化时使用，
# 请使用如下方式调用：g.conf.xxx
# 而不是在模块预先定义全局变量再调用
# <<< 建议
